package com.cyk.demo.controller;

import com.cyk.demo.service.UserService;
import com.cyk.model.user.Userinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")  // 之前不知道为什么，现有的配置保持不变，把这一行删了就可以
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/hi")
    public String sayHi() {
        return "hello world";
    }

    @RequestMapping("/login")
    public int login(String username, String password) {
        //1.非空校验
        if(!StringUtils.hasLength(username) && !StringUtils.hasLength(password)) {
            return -1;
        }
        //2.验证正确性
        Userinfo dbUser = userService.selectUserinfo(username, password);
        if(dbUser == null || !dbUser.getUsername().equals(username) &&
                !dbUser.getPassword().equals(password)) {
            return -1;
        }
        return 1;
    }


}
